<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"inkgn.gitee.io","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="分支篇 git本地分支与远程分支关联  此情景为本地已创建develop分支,且远程已创建develop分支 命令:git branch –set-upstream-to&#x3D;origin&#x2F;develop(远程分支名) develop(本地分支名) 通过此命令后,可直接使用git pull 拉取分支最新内容   git 克隆指定分支代码  当需要克隆指定分支的代码时可以使用此命令 命令:git clo">
<meta property="og:type" content="article">
<meta property="og:title" content="git  | git常见问题集合(本地分支和远程分支的关联)">
<meta property="og:url" content="https://inkgn.gitee.io/2022/04/13/article37/index.html">
<meta property="og:site_name" content="静水深流">
<meta property="og:description" content="分支篇 git本地分支与远程分支关联  此情景为本地已创建develop分支,且远程已创建develop分支 命令:git branch –set-upstream-to&#x3D;origin&#x2F;develop(远程分支名) develop(本地分支名) 通过此命令后,可直接使用git pull 拉取分支最新内容   git 克隆指定分支代码  当需要克隆指定分支的代码时可以使用此命令 命令:git clo">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-04-13T03:49:11.000Z">
<meta property="article:modified_time" content="2022-04-13T03:49:11.000Z">
<meta property="article:author" content="yinks">
<meta property="article:tag" content="git">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://inkgn.gitee.io/2022/04/13/article37/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>git  | git常见问题集合(本地分支和远程分支的关联) | 静水深流</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">静水深流</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/categories" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-motto">

    <a href="/motto/motto" rel="section"><i class="fa fa-book fa-fw"></i>古训</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/me" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://inkgn.gitee.io/2022/04/13/article37/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/user_info/face">
      <meta itemprop="name" content="yinks">
      <meta itemprop="description" content="长路漫漫，半程风雨，半程春">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="静水深流">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          git  | git常见问题集合(本地分支和远程分支的关联)
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-04-13 11:49:11" itemprop="dateCreated datePublished" datetime="2022-04-13T11:49:11+08:00">2022-04-13</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h4 id="分支篇"><a href="#分支篇" class="headerlink" title="分支篇"></a>分支篇</h4><ul>
<li><p>git本地分支与远程分支关联</p>
<blockquote>
<p>此情景为本地已创建<code>develop</code>分支,且远程已创建<code>develop</code>分支</p>
<p>命令:<strong>git branch –set-upstream-to=origin/develop(远程分支名) develop(本地分支名)</strong></p>
<p>通过此命令后,可直接使用<code>git pull </code>拉取分支最新内容</p>
</blockquote>
</li>
<li><p>git 克隆指定分支代码</p>
<blockquote>
<p>当需要克隆指定分支的代码时可以使用此命令</p>
<p>命令:<strong>git clone -b v2.8.1(分支名称) <a target="_blank" rel="noopener" href="https://git.xxx.git/">https://git.xxx.git</a></strong></p>
</blockquote>
</li>
<li><p>2.如果本地新建了一个分支<code>branch_name</code>，但是在远程没有</p>
<blockquote>
<p>如果本地新建了一个分支<code>branch_name</code>，但是在远程没有，这时候<code>push</code>和<code>pull</code>指令就无法确定该跟踪谁,一般来说我们都会使其跟踪远程同名分支，所以可以利用<code>git push --set-upstream origin branch_name</code>，这样就可以自动在远程创建一个<code>branch_name</code>分支，然后本地分支会<code>track</code>该分支。后面再对该分支使用<code>push</code>和<code>pull</code>就自动同步。无需再指定分支。</p>
</blockquote>
</li>
<li><p>如果远程新建了一个分支，本地没有该分支</p>
<blockquote>
<p>1）如果远程新建了一个分支，本地没有该分支，可以用<code>git checkout --track origin/branch_name</code>，这时候本地会新建一个分支名叫<code>branch_name</code>，会自动跟踪远程的同名分支<code>branch_name</code>。<br>2）用上面中方法，得到的分支名永远和远程的分支名一样，如果想新建一个本地分支不同名字，同时跟踪一个远程分支可以利用。<br><code>git checkout -b new_branch_name branch_name</code>，这条指令本来是根据一个<code>branch_name</code>分支分出一个本地分支<code>new_branch_name</code>，但是如果所根据的分支<code>branch_name</code>是一个远程分支名，那么本地的分支会自动的track远程分支。建议跟踪分支和被跟踪远程分支同名。</p>
</blockquote>
</li>
</ul>
<ul>
<li><p>git新的远程分支同步到本地</p>
<blockquote>
<p>命令:<strong>git checkout -b localnewbranchname origin/remotenewbranchname</strong></p>
</blockquote>
<p>*<em>总结：一般我们就用*</em><code>git push --set-upstream origin branch_name</code>*<em>来在远程创建一个与本地<code>branch_name</code>同名的分支并跟踪；利用*</em><code>git checkout --track origin/branch_name</code>*<em>来在本地创建一个与<code>branch_name</code>同名分支跟踪远程分支</em>*</p>
</li>
</ul>
<h4 id="tag篇"><a href="#tag篇" class="headerlink" title="tag篇"></a>tag篇</h4><ul>
<li><p>给版本添加tag</p>
<blockquote>
<p>给一个commit添加tag有时候是非常有用的，例如当程序开发到了一个阶段性的完成，此时我们可能就需要对当前版本做出标记，常用的commit 已经不太方便了，因为后期查找时一定让人抓狂，单独拉一个分支出来？可行，但似乎有些大材小用了，而且对于多人开发来讲还是会有些许不便，那么我们就可以用到tag了。</p>
<p>1）新建tag</p>
<p>基础命令：<strong>git tag v1.0.0</strong>  通过此命令可以对当前版本添加一个tag,我们可以通过<strong>git tag</strong> 命令查看当前有多少个tag,如果要查看详细信息可以使用<strong>git show tagName</strong> 查看 例如：<strong>git show v1.0.0</strong> </p>
<p>给tag带上备注可以使用命令 <strong>git tag -a tagName -m “备注信息”</strong> </p>
<p>2）给历史commit 添加tag</p>
<p>当我们经过了几次commit后发现忘记添加tag怎么办？别担心，git还能给历史commit添加tag</p>
<p>通过命令：<strong>git tag -a v1.1.0 commit序列号前几位 -m “tag 备注”</strong></p>
<p>3）将tag提交到远程</p>
<p>我们可以通过一下命令将tag提交到远端：<strong>git push origin tagName</strong></p>
<p>4）切换到某个tag</p>
<p>跟分支一样，可以直接切换到某个tag去。这个时候不位于任何分支，处于游离状态，可以考虑基于这个tag创建一个分支 <strong>git checkout v1.0.0</strong> </p>
<p>5）本地删除某个tag</p>
<p><strong>git tag -d v1.0.0</strong></p>
<p>6）远端删除</p>
<p><strong>git push origin :refs/tags/v1.0.0</strong> </p>
</blockquote>
</li>
</ul>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>yinks
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://inkgn.gitee.io/2022/04/13/article37/" title="git  | git常见问题集合(本地分支和远程分支的关联)">https://inkgn.gitee.io/2022/04/13/article37/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/git/" rel="tag"># git</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/02/08/article36/" rel="prev" title="git  | git常见问题及常见命令">
      <i class="fa fa-chevron-left"></i> git  | git常见问题及常见命令
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/12/06/article38/" rel="next" title="git  | 不同分支提交到不同的远程仓库">
      git  | 不同分支提交到不同的远程仓库 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%88%86%E6%94%AF%E7%AF%87"><span class="nav-number">1.</span> <span class="nav-text">分支篇</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#tag%E7%AF%87"><span class="nav-number">2.</span> <span class="nav-text">tag篇</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="yinks"
      src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/user_info/face">
  <p class="site-author-name" itemprop="name">yinks</p>
  <div class="site-description" itemprop="description">长路漫漫，半程风雨，半程春</div>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://gitee.com/inkgn" title="Gitee → https:&#x2F;&#x2F;gitee.com&#x2F;inkgn" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>Gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="/2087104694@qq.com" title="E-Mail → 2087104694@qq.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">yinks</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
